package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.patient.PatientAddReqDTO;
import com.yixin.hospital.entity.dto.req.patient.PatientEditReqDTO;
import com.yixin.hospital.entity.dto.req.patient.PatientVipSettingReqDTO;
import com.yixin.hospital.service.PatientService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/patient")
@Api(value = "患者信息管理", tags = "患者信息商管理")
public class PatientController {

    @Resource
    private PatientService patientService;


    @GetMapping("/list")
    @ApiOperation(value = "患者列表")
    public ApiResult<?> list(@RequestParam int currentPage, @RequestParam int pageSize, String name, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return patientService.allList(currentPage, pageSize, name, userId);
    }

    @GetMapping("/allList")
    @ApiOperation(value = "患者下拉框")
    public ApiResult<?> allList() {
        return patientService.allDropList();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加患者")
    public ApiResult<?> add(@Valid @RequestBody PatientAddReqDTO patientAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientService.add(patientAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑患者")
    public ApiResult edit(@Valid @RequestBody PatientEditReqDTO patientEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientService.edit(patientEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除患者")
    public ApiResult remove(@PathVariable int id) {
        patientService.removeById(id);
        return ApiResult.success();
    }

    @GetMapping("/vipAllList")
    @ApiOperation(value = "会员等级设置查询所有会员")
    public ApiResult vipAllList(@RequestParam("selectedLevel") Integer selectedLevel) {
        return ApiResult.success(patientService.settingVip(selectedLevel));
    }

    @PostMapping("/updateVip")
    @ApiOperation(value = "会员等级设置设置vip")
    public ApiResult updateVip(@Valid @RequestBody PatientVipSettingReqDTO patientVipSettingReqDTO) {
        patientService.updateVip(patientVipSettingReqDTO);
        return ApiResult.success();
    }

}
